/*******************************************************************************
 * 
 * Copyright:
 * 
 * License:
 * 
 * Authors: SALMON Charles
 * 
 ******************************************************************************/

/*******************************************************************************
 * 
 ******************************************************************************/

qx.Class.define("bip.ui.form.survey.QuestionField",
{
  extend : qx.ui.core.Widget,
  
  implement : [
               qx.ui.form.INumberForm,
               qx.ui.form.IForm
             ],
  include : [
     qx.ui.form.MForm
  ],

  construct : function(minValue,maxValue, textValues)
  {
  	if (qx.core.Environment.get("qx.debug")) {
      if (minValue == null) { throw new Error("minValue required");}
      if (maxValue == null) { throw new Error("maxValue required");}
    }
    this.base(arguments);
    this._minValue = minValue;
    this._maxValue = maxValue;
    
    // Init text values
    this._textValues = textValues;
    if (this._textValues == null) {this._textValues = new qx.data.Array();}
    
    // initialize the layout
    var layout = new qx.ui.layout.Grid();
    this._setLayout(layout);
    layout.setColumnFlex(0,1);
    layout.setRowAlign(0,"left","middle");
    layout.setRowAlign(1,"left","top");
    
    // create the widgets
    this._createChildControl("help-button");
    this._createChildControl("name");
    this._createChildControl("description");
    var field = this._createChildControl("field");
    field.bind("value",this,"value");
  },
  
  /*
   *****************************************************************************
      EVENTS
   *****************************************************************************
   */
  events :
  {
    /** Fired when the value was modified */
    "changeValue" : "qx.event.type.Data"
  },
   
   /*
    *****************************************************************************
       PROPERTIES
    *****************************************************************************
    */
   
	  properties :
	  {
	  	appearance :
	    {
	      refine : true,
	      init : "question-field"
	    },
	    
	    description :
	    {
	      check : "String",
	      apply : "_applyDescription",
	      init : "Description"
	    },
	    
	    name :
	    {
	      check : "String",
	      apply : "_applyName",
	      init : "Question ?"
	    },
	    
	    value :
	    {
	      check : "Integer",
	      nullable : true,
	      event : "changeValue",
	      apply : "_applyValue"
	    }
	   
	  },
	  
	  /*
	   *****************************************************************************
	      MEMBERS
	   *****************************************************************************
	   */
  members :
  { 
  	_minValue: null,
  	_maxValue: null,
  	_textValues: null,
  	
    // overridden
    _createChildControlImpl : function(id)
    {
      var control;

      switch(id)
      {      
      
	      case "name":
	        control = new qx.ui.basic.Label(this.getName());
	        control.setAnonymous(true);
	        control.setRich(true);
	        control.setAllowGrowX(true);
	        this._add(control,{row:0,column:0});
	        break;
	        
	      case "help-button":
	        control = new qx.ui.toolbar.CheckBox(null, "icon/16/status/dialog-information.png");
	        control.setHeight(0);
	        control.addListener("changeValue",function(evt) {
	        	if (evt.getData()) {
	        		this.getChildControl("description").show();
	        	} else {
	        		this.getChildControl("description").exclude();
	        	}
	        },this);
	        this._add(control,{row:0,column:1});
	        break;
        
        case "description":
          control = new qx.ui.basic.Label(this.getDescription());
          control.setAnonymous(true);
          control.setRich(true);
          control.setAllowGrowX(true);
          control.exclude();
          this._add(control,{row:1,column:0,colSpan:2});
          break;
        
        case "field":
          control = bip.ui.form.survey.AnswerFieldBuilder.build(this.getMinValue(),this.getMaxValue(), this.getTextValues());
          control.setPaddingLeft(20);
          control.setAllowGrowX(true);
          
          this._add(control,{row:2,column:0,colSpan:2});
          break;
      }

      return control || this.base(arguments, id);
    },
    
    // property apply
    _applyDescription : function(value, old)
    {
      var desc = this.getChildControl("description");
      desc.setValue(value);
    },
    
    _applyName : function(value, old)
    {
      var name = this.getChildControl("name");
      name.setValue(value);
    },
    
    _applyValue: function(value,old) {
    	this.getChildControl("field").setValue(value);
    },
    
    getMinValue: function() {
    	return this._minValue;
    },
    
    getMaxValue: function() {
    	return this._maxValue;
    },
    
    getTextValues: function() {
    	return this._textValues;
    }
  },
  
  destruct : function()
  {
  	this._disposeObjects("_textValues");
  }
});